﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations; 

namespace VideoClassLibrary.CacheManager
{
  public  class CommonCacheManager
    {
        public static T GetCacheData<T>(string key, Func<T> func)
        {
            ICacheManager primitivesCache = CacheFactory.GetCacheManager();
            if (!primitivesCache.Contains(key))
            {
                T data = func();
                primitivesCache.Add(key, data);
            }
            return (T)primitivesCache.GetData(key);
        }



        public static void AddCacheData<T>(string key, T data, double slidingMinutes)
        {
            ICacheManager primitivesCache = CacheFactory.GetCacheManager();
            primitivesCache.Add(key, data, CacheItemPriority.High, null, new SlidingTime(TimeSpan.FromMinutes(slidingMinutes)));

        }

        public static T GetCacheData<T>(string key)
        {
            ICacheManager primitivesCache = CacheFactory.GetCacheManager();
            return (T)primitivesCache.GetData(key);
        }

        public static void FlushCache()
        {
            ICacheManager primitivesCache = CacheFactory.GetCacheManager();

            primitivesCache.Flush();

        }

        public static void Remove(string key)
        {
            ICacheManager primitivesCache = CacheFactory.GetCacheManager();

            primitivesCache.Remove(key);

        }


        public static T GetTenMinutesCacheData<T>(string key, Func<T> func)
        {
            ICacheManager primitivesCache = CacheFactory.GetCacheManager();
            if (!primitivesCache.Contains(key))
            {
                T data = func();
                primitivesCache.Add(key, data,  CacheItemPriority.High, null, new SlidingTime(TimeSpan.FromMinutes(10)));
            }
            return (T)primitivesCache.GetData(key);
        }





        public static T GetCacheData<T>(string key, string id, Func<string, T> func, double slidingMinutes)
        {
            ICacheManager primitivesCache = CacheFactory.GetCacheManager();
            if (!primitivesCache.Contains(key))
            {
                T data = func(id);
                primitivesCache.Add(key, data, CacheItemPriority.High, null, new SlidingTime(TimeSpan.FromMinutes(slidingMinutes)));
            }
            return (T)primitivesCache.GetData(key);
        }
    }

}
